package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/***
 * title AdServiceImpl
 * @description <TODO description class purpose>
 * @author ZCZ
 * @version 1.0.0
 * @create 2022/9/7 20:59
 **/
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public CommonData<MarketAd> getAllAdInfo(BasePageInfo info, String name, String content) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAdExample example = new MarketAdExample();
        example.setDistinct(true);
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        //条件查询
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null) {
            criteria.andContentLike("%" + content + "%");
        }


        List<MarketAd> marketAdList = marketAdMapper.selectByExample(example);
        CommonData<MarketAd> marketAdCommonData = new CommonData<>();
        marketAdCommonData.setLimit(info.getLimit());
        marketAdCommonData.setPage(info.getPage());

        PageInfo pageInfo = new PageInfo(marketAdList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketAdCommonData.setPage(pages);
        marketAdCommonData.setTotal(total);
        marketAdCommonData.setList(marketAdList);

        return marketAdCommonData;
    }

    @Override
    public MarketAd creat(MarketAd marketAd) {
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        marketAdMapper.insertSelective(marketAd);
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(marketAd.getId());

        return marketAd1;
    }

    @Override
    public void delete(Integer marketAdId) {
        // marketAdMapper.deleteByPrimaryKeyLogic(marketAdId);
        MarketAd marketAd = marketAdMapper.selectByPrimaryKey(marketAdId);
        marketAd.setUpdateTime(new Date());
        marketAd.setDeleted(true);
        marketAdMapper.updateByPrimaryKey(marketAd);
    }

    //响应格式也不对。
    @Override
    public MarketAd update(MarketAd marketAd) {
        marketAd.setUpdateTime(new Date());
        marketAdMapper.updateByPrimaryKey(marketAd);
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(marketAd.getId());
        return marketAd1;
    }
}
